package com.xinqi.modules.file.course.courseware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinqi.common.base.enums.YesNoNumberEnum;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.file.course.courseware.domain.CourseResourceEntity;
import com.xinqi.modules.file.course.courseware.domain.CourseResourcePartEntity;
import com.xinqi.modules.file.course.courseware.mapper.CourseResourceMapper;
import com.xinqi.modules.file.course.courseware.mapper.CourseResourcePartMapper;
import com.xinqi.modules.file.course.courseware.service.CourseResourcePartService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class CourseResourcePartServiceImpl extends BaseServiceImpl<CourseResourcePartMapper, CourseResourcePartEntity> implements CourseResourcePartService {

    private final CourseResourceMapper resourceMapper;
    private final CourseResourcePartMapper resourcePartMapper;

    @Override
    public void updateResource(Long contentId) {
        CourseResourceEntity entity = new CourseResourceEntity();
        entity.setId(contentId);
        entity.setPartReleaseStatus(YesNoNumberEnum.YES.getCode());
        resourceMapper.updateById(entity);
    }

    @Override
    public CourseResourceEntity findResource(Long contentId) {
        return resourceMapper.selectById(contentId);
    }

    @Override
    public void deletePartsByContentId(Long contentId) {
        LambdaQueryWrapper<CourseResourcePartEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseResourcePartEntity::getContentId, contentId);
        List<CourseResourcePartEntity> list = resourcePartMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            resourcePartMapper.deletePartsByIds(list.stream().map(CourseResourcePartEntity::getId).collect(Collectors.toList()));
        }
    }

}
